/**
 * 
 */
package com.sc.softmgr.business.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.acc.softmgr.model.SysUser;
import com.acc.softmgr.model.SysUserExample;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.sc.softmgr.business.service.ISysUserService;
import com.sc.softmgr.common.constant.DeleteStatus;
import com.sc.softmgr.common.constant.enums.RestStatus;
import com.sc.softmgr.common.utils.date.DateBase;
import com.sc.softmgr.common.web.DataGridObject;

/**
 * 
 * @content
 * 
 * @date 2017年5月12日
 * 
 * @author OprCalf
 * 
 */
@Controller
@RequestMapping("sysUser")
public class SysUserController {

	@Autowired
	private ISysUserService sysUserService;

	@RequestMapping(value = "toSysUser", method = RequestMethod.GET)
	public String toSysUser() {
		return "sysUser/sysUser";
	}

	/**
	 * 账号密码验证
	 * 
	 * @return
	 */
	@HystrixCommand
	@ResponseBody
	@RequestMapping(value = "checkUser", method = RequestMethod.POST)
	public JSONObject checkUser(SysUser user, HttpServletRequest request) {
		SysUserExample example = new SysUserExample();
		JSONObject json = new JSONObject();
		example.createCriteria().andUsernameEqualTo(user.getUsername()).andPasswordEqualTo(user.getPassword());
		List<SysUser> list = sysUserService.selectByExample(example);
		if (list != null && list.size() > 0) {
			request.getSession().setAttribute("currentUser", list.get(0));
			json.put("status", RestStatus.EXIT_USER.code);
			json.put("message", RestStatus.EXIT_USER.message);
		} else {
			json.put("status", RestStatus.NO_EXIT_USER.code);
			json.put("message", RestStatus.NO_EXIT_USER.message);
		}
		return json;
	}

	/**
	 * 查询列表页
	 * 
	 * @param username
	 * @param userEmail
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "searchSysUsers", method = RequestMethod.GET)
	@ResponseBody
	public String searchSysUsers(SysUser sysUser, @RequestParam Integer page, @RequestParam Integer rows,
			HttpServletRequest request) throws Exception {
		DataGridObject<SysUser> responseObject = new DataGridObject<SysUser>();
		SysUserExample example = new SysUserExample();
		if (!StringUtils.isEmpty(sysUser.getUsername())) {
			example.getCriteria().andUsernameEqualTo(sysUser.getUsername());
		}
		if (page == null || page == 0) {
			page = 1;
		}
		SysUser userNameSession = (SysUser) request.getSession().getAttribute("currentUser");
		if (userNameSession != null && StringUtils.isEmpty(userNameSession.getUsername())) {
			example.getCriteria().andCreatebyEqualTo(userNameSession.getUsername());
		}

		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		example.setCount(page * rows);
		example.setLimitStart((page - 1) * rows);

		List<SysUser> sysUsers = sysUserService.selectByExample(example);
		for (SysUser user : sysUsers) {
			user.setCreateTimestring(DateBase.formatDate(user.getCreatetime(), DateBase.DATE_PATTERN_DATETIME));
		}
		responseObject.setTotal(page * rows);
		responseObject.setRows(sysUsers);

		return JSON.toJSONString(responseObject);
	}

	/**
	 * 查询详情
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/{id}/searchPsUser", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject searchUser(@PathVariable Integer id) throws Exception {
		JSONObject json = new JSONObject();
		if (id == null || id == 0) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		}
		SysUser sysUser = sysUserService.selectByPrimaryKey(id);
		json.put("status", RestStatus.SUCCESS.code);
		json.put("message", RestStatus.SUCCESS.message);
		json.put("data", sysUser);
		return json;
	}

	/**
	 * 新增用户
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addSysUser", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject addSysUser(SysUser user, HttpServletRequest request) throws Exception {
		JSONObject json = new JSONObject();
		if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())
				|| StringUtils.isEmpty(user.getUser_email())) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		}

		// ----------判断唯一性------------
		SysUserExample example = new SysUserExample();
		example.getCriteria().andPasswordEqualTo(user.getPassword());
		example.getCriteria().andUsernameEqualTo(user.getUsername());
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		List<SysUser> sysUsers = sysUserService.selectByExample(example);
		if (sysUsers != null && sysUsers.size() > 0) {
			json.put("status", RestStatus.NOT_UNIQUE.code);
			json.put("message", RestStatus.NOT_UNIQUE.message);
			return json;
		} else {
			SysUser sysUser = new SysUser();
			sysUser.setCreatetime(new Date());
			SysUser userNameSession = (SysUser) request.getSession().getAttribute("currentUser");
			if (userNameSession != null && StringUtils.isNotEmpty(userNameSession.getUsername())) {
				sysUser.setCreateby(
						StringUtils.isEmpty(userNameSession.getUsername()) ? "root" : userNameSession.getUsername());
			}
			sysUser.setPassword(user.getPassword());
			sysUser.setUser_email(user.getUser_email());
			sysUser.setUsername(user.getUsername());
			sysUser.setDeleted(DeleteStatus.FALSE);
			sysUserService.insert(sysUser);
			json.put("status", RestStatus.SUCCESS.code);
			json.put("message", RestStatus.SUCCESS.message);
		}
		return json;
	}

	/**
	 * 修改用户
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "editSysUser", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject editSysUser(SysUser user, HttpServletRequest request) throws Exception {
		JSONObject json = new JSONObject();
		if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getUser_email())
				|| StringUtils.isEmpty(user.getPassword()) || user.getId() == null || user.getId() == 0) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		}
		SysUser oldUser = sysUserService.selectByPrimaryKey(user.getId());
		if (oldUser != null && DeleteStatus.FALSE.equals(oldUser.getDeleted())) {
			oldUser.setUsername(user.getUsername());
			oldUser.setUser_email(user.getUser_email());
			SysUser userNameSession = (SysUser) request.getSession().getAttribute("currentUser");
			if (userNameSession != null && StringUtils.isNotEmpty(userNameSession.getUsername())) {
				oldUser.setUpdateby(userNameSession.getUsername());
			}
			oldUser.setUpdatetime(new Date());
			sysUserService.updateByPrimaryKey(oldUser);
			json.put("status", RestStatus.SUCCESS.code);
			json.put("message", RestStatus.SUCCESS.message);
			return json;
		} else {
			json.put("status", RestStatus.NO_EXIT_USER.code);
			json.put("message", RestStatus.NO_EXIT_USER.message);
			return json;
		}
	}

	/**
	 * 删除用户
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/removeSysUser", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject removeSysUser(@RequestParam Integer id) throws Exception {
		JSONObject json = new JSONObject();
		if (id == null || id == 0) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		}
		SysUser sysUser = sysUserService.selectByPrimaryKey(id);
		sysUser.setDeleted(DeleteStatus.TRUE);
		sysUserService.updateByPrimaryKey(sysUser);
		json.put("status", RestStatus.SUCCESS.code);
		json.put("message", RestStatus.SUCCESS.message);
		return json;
	}
}
